<?php
namespace app\bancard\controller;

use app\common\lib\Geet;
use think\controller;

class Login extends Common{
	
	//解决继承Base导致的死循环，初始化这个方法
    protected function initialize(){}

    //登录页面
    public function index(){
		if(request()->isPost()){

            $data = input('post.');
            try{
                $user = db('admin')->where(['username'=>$data['username']])->find();
            }catch(\Exception $e){
                $this->error($e->getMessage());
            }

            if(!$user){
                return show(1, '用户不存在', [], 200);
            }
            if(md5($data['password']) != $user['password']){
                return show(1, '密码不正确', [], 200);
            }
            //更新数据库，登录时间，登录ip
            $update = [
                'login_ip'  => request()->ip(),
                'login_time'=> time()
            ];
            try{
                db('admin')->where(['id'=>$user['id']])->update($update);
            }catch(\Exception $e){
                $this->error($e->getMessage());
            }
            session('admin',$user);

            return show(0, '登录成功', $user, 200);
        }else{
            if($this->isLogin()){
                $this->redirect('bancard/index/index');
            }
            return $this->fetch();
        }
	}
	
	/**
     * 极验验证
     */
    public function geet_code(){
        $geetest_id=config('code.GEETEST_ID');
        $geetest_key=config('code.GEETEST_KEY');
        $GtSdk = new Geet($geetest_id, $geetest_key);
        $user_id = array('user_id'=>'test1');
        $status = $GtSdk->pre_process($user_id);
        $_SESSION['geetest']=array(
            'gtserver'=>$status
        );
        echo $GtSdk->get_response_str();
    }
	
}